﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DLL;

namespace BLL
{
    public class NhanVienManager
    {
        public List<NHAN_VIEN> GetAll()
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = from nv in db.NHAN_VIEN
                        select nv;
            return query.ToList();
        }

        public bool ThemNV(NHAN_VIEN obj)
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = from nv in db.NHAN_VIEN
                         where nv.MaNhanVien.Equals(obj.MaNhanVien)
                         select nv;
            if (!query.Any())
            {
                db.NHAN_VIEN.Add(obj);
                db.SaveChanges();
                return true;
            }
            else
                return false;
        }

        public bool CapNhatNV(NHAN_VIEN obj)
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = (from nv in db.NHAN_VIEN
                        where nv.MaNhanVien.Equals(obj.MaNhanVien)
                        select nv).FirstOrDefault();
            if (query!=null)
            {
                query.TenNhanVien = obj.TenNhanVien;
                query.SoDienThoai = obj.SoDienThoai;
                query.SoCMND = obj.SoCMND;
                query.NamSinh = obj.NamSinh;
                query.GioiTinh = obj.GioiTinh;
                query.Email = obj.Email;
                query.DiaChi = obj.DiaChi;
                db.SaveChanges();
                return true;
            }
            else
                return false;
        }

        public bool XoaNV(string MaNhanVien)
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = (from nv in db.NHAN_VIEN
                         where nv.MaNhanVien  == MaNhanVien
                         select nv).FirstOrDefault<NHAN_VIEN>();
            if (query != null)
            {
                db.NHAN_VIEN.Remove(query);
                db.SaveChanges();
                return true;
            }
            else
                return false;
        }

        public List<NHAN_VIEN> TimNV_Ma(string MaNhanVien)
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = from nv in db.NHAN_VIEN
                        where nv.MaNhanVien == MaNhanVien
                        select nv;
            return query.ToList();
        }

        public List<NHAN_VIEN> TimNV_Ten(string TenNhanVien)
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = from nv in db.NHAN_VIEN
                         where nv.TenNhanVien.Contains(TenNhanVien)
                         select nv;
            return query.ToList();
        }

        public List<NHAN_VIEN> TimNV_GioiTinh(string GioiTinh)
        {
            CuaHangBangDiaEntities db = new CuaHangBangDiaEntities();
            var query = from nv in db.NHAN_VIEN
                        where nv.GioiTinh == GioiTinh
                        select nv;
            return query.ToList();
        }
    }
}
